﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TSVNAddin.Core
{
	public class ItemCommandContext : IPathCommandContext
	{
		private readonly WorkingContext _WorkingContext;
		private readonly SelectedItemsAdapter _SelectedItemsAdapter;
		private readonly IItemCompatibility _ItemCompatibility;

		public ItemCommandContext(SelectedItemsAdapter selectedItemsAdapter, WorkingContext workingContext, IItemCompatibility itemCompatibility)
		{
			_SelectedItemsAdapter = selectedItemsAdapter;
			_WorkingContext = workingContext;
			_ItemCompatibility = itemCompatibility;
		}

		public CommandStatus GetCommandStatus()
		{
			if (!_ItemCompatibility.IsCompatible(_SelectedItemsAdapter))
				return CommandStatus.Invisible;

			var paths = _SelectedItemsAdapter.GetSelectedPaths();
			if (!paths.TrueForAll(path => _WorkingContext.CanExecuteCommandsOnPath(path)))
				return CommandStatus.Disabled;

			return CommandStatus.Enabled;
		}

		public string GetWorkingPath()
		{
			List<string> itemPaths = _SelectedItemsAdapter.GetSelectedPaths();
			
			string path = string.Empty;
			var strBuilder = new StringBuilder(path);
			itemPaths.ForEach(itemPath => strBuilder.Append(itemPath).Append('*'));

			return strBuilder.ToString().TrimEnd('*');
		}
	}
}
